package at.spengergasse.web.servlet.produkte;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import at.spengergasse.dao.ProdukteDao;
import at.spengergasse.dao.derby.ProdukteDaoDerbyImpl;
import at.spengergasse.web.EntryPointURLEnum;
import at.spengergasse.web.servlet.LoginServlet;

@SuppressWarnings("serial")
public abstract class AbstractProduktServlet extends LoginServlet
{

	public AbstractProduktServlet() throws Exception
	{

	}

	protected static ProdukteDao produkteDao = new ProdukteDaoDerbyImpl();

	protected Long checkProdukteIdParam(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException
	{
		Long produkteId = null;
		try
		{
			produkteId = Long.valueOf(req.getParameter(ProdukteFieldNames.ID));
		} catch (Exception e)
		{
			log.warn("problems with given produkteId!", e);
		}

		if (produkteId == null)
		{
			redirectToProdukteList(req, resp, "wrong produkteId");
			return null;
		}
		return produkteId;
	}

	protected void redirectToProdukteList(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException
	{
		redirectToProdukteList(req, resp, null);
	}

	protected void redirectToProdukteList(HttpServletRequest req,
			HttpServletResponse resp, String errorMessage)
			throws ServletException, IOException
	{
		if (errorMessage != null)
		{
			req.getSession(true).setAttribute("errorMessage", errorMessage);
		}
		resp.sendRedirect(EntryPointURLEnum.PRODUKTE_LIST_ALL.getUrl());
	}

}
